package com.iss.umservice.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.iss.iframework.util.StringUtil;
import com.iss.iframework.web.ResultCall;
import com.iss.iframework.web.support.tld.pagination.Navigation;
import com.iss.iframework.web.support.tld.pagination.PageUtil;
import com.iss.iframework.web.support.tld.pagination.Pagination;
import com.iss.umservice.bean.AreaBean;
import com.iss.umservice.manager.AreaManager;

/**
 * 区域管理控制器
 * 
 * @author suxiaoping
 * @date 2012-9-20
 */

@Controller
@RequestMapping("/area/*")
public class AreaController
{
    @Autowired
    private AreaManager areaManager;
    
    /**
     * 查询区域信息
     * 
     * @param areaName 区域名称
     * @param pages 页数
     * @param model map
     * @return
     */
    @RequestMapping
    public void queryAreaInfo(String areaName, String pages,
            Map<String, Object> model)
    {
        
        int count = areaManager.countAreaInfos(areaName);
        
        Pagination pagination = PageUtil.createPage(pages,
                count,
                "../area/queryAreaInfo.html");
        
        // 从会话中获取区域对象
        List<AreaBean> list = areaManager.queryAreaInfo(areaName,
                pagination.getOffset(),
                pagination.getRowSize());
        
        //条件放入分页当中
        pagination.addCondition("areaName", areaName);
        
        model.put("areaBean", list);
        
        model.put(Navigation.PAGINATION, pagination);
    }
    
    /**
     * 修改区域信息
     * 
     * @param bean 岗位实体类
     * @param model map
     * @return
     */
    @RequestMapping
    public void modifyAreaInfo(AreaBean bean)
    {
        //删除岗位信息
        areaManager.modifyAreaInfo(bean);
        
        ResultCall.makeSuccess("../area/queryAreaInfo.html");
        
    }
    
    /**
     * 增加区域信息
     * @param bean 区域实体类
     * @return
     */
    @RequestMapping
    public void addAreaInfo(AreaBean bean)
    {
        //生成AREAID
        String areaId = StringUtil.getPrimarySessionId();
        
        bean.setAreaId(areaId);
        
        //添加区域信息
        areaManager.addAreaInfo(bean);
        
        ResultCall.makeSuccess("../area/queryAreaInfo.html");
        
    }
    
    /**
     * 跳转界面
     * @return
     */
    @RequestMapping
    public void insertAreaInfo()
    {
        
    }
    
    /**
     * 修改跳转界面
     * @param areaId 区域ID
     * @return
     */
    @RequestMapping
    public String updateAreaInfo(String areaId, Map<String, Object> model)
    {
        AreaBean bean = areaManager.queryAreaById(areaId);
        
        model.put("areaBean", bean);
        
        return "/area/modifyAreaInfo";
    }
    
    /**
     * 删除区域信息
     * 
     * @param areaId 区域Id
     * @return
     */
    @RequestMapping
    public void delAreaInfo(String areaId)
    {
        //删除区域信息
        areaManager.delAreaInfoById(areaId);
        
        ResultCall.makeSuccess("../area/queryAreaInfo.html");
        
    }
    
    
    /**
     * 校验区域名称是否已存在
     * @param stationName
     * @param response
     * @throws UnsupportedEncodingException
     */
    @RequestMapping
    public void checkAreaCode(String areaCode, HttpServletResponse response) throws UnsupportedEncodingException
    {
        areaCode = URLDecoder.decode(areaCode,"UTF-8");
       String isExist = areaManager.checkAreaCode(areaCode);
       PrintWriter writer = null;
       try
       {
           response.setCharacterEncoding("UTF-8");
           writer = response.getWriter();
           writer.write(isExist);
       }
       catch (Exception e) {
    }
       
    }
}
